<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Http\Session\HttpSession;

/**
 * Class SwoftSessionController
 *
 * @Controller(prefix="/Session")
 */
class SwoftSessionController
{
    /**
     * @RequestMapping(route="set1")
     * @param Response $response
     *
     * @return Response
     */
    public function set1(Response $response)
    {
        $sess  = HttpSession::current();
        $times = $sess->get('times', 0);
        $times++;

        $sess->set('times', $times);
        return [
            'times'  => $times,
            'sessId' => $sess->getSessionId()
        ];
    }

    /**
     * @RequestMapping(route="set2")
     * @param Response $response
     *
     * @return Response
     */
    public function set2(Response $response)
    {
        $sess = HttpSession::current();
        $sess->set('testKey', 'test-value');
        $sess->set('testKey1', ['k' => 'v', 'v1', 3]);

        return ['testKey', 'testKey1'];
    }

    /**
     * @RequestMapping(route="get")
     *
     * @return array
     */
    public function get()
    {
        $sess = HttpSession::current();
        return ['get.testKey' => $sess->get('testKey')];
    }

    /**
     * @RequestMapping(route="get_all")
     *
     * @return array
     */
    public function getAll()
    {
        $sess = HttpSession::current();
        return $sess->toArray();
    }

    /**
     * @RequestMapping(route="del")
     *
     * @return array
     */
    public function del()
    {
        $sess = HttpSession::current();
        $ok   = $sess->delete('testKey');
        return ['delete' => $ok];
    }

    /**
     * @RequestMapping(route="close")
     * @param Response $response
     *
     * @return Response
     */
    public function close(Response $response)
    {
        $sess = HttpSession::current();

        return ['destroy' => $sess->destroy()];
    }

    /**
     * @RequestMapping(route="not")
     *
     * @return string
     */
    public function not()
    {
        return 'not-use';
    }

    /**
     * @RequestMapping(route="flash")
     *
     * @return array
     */
    public function flash()
    {
        $sess = HttpSession::current();
        $sess->setFlash('flash1', 'test-value');

        return ['set.testKey' => 'test-value'];
    }
}
